package com.ruoyi.zyx.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.ZyxDevice;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.zyx.domain.ZyxSendorder;
import com.ruoyi.zyx.mapper.ZyxSendorderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zyx.mapper.ZyxDeviceMapper;
import com.ruoyi.zyx.service.IZyxDeviceService;

/**
 * 设备基础信息Service业务层处理
 *
 * @author lhs
 * @date 2021-05-26
 */
@Service
public class ZyxDeviceServiceImpl implements IZyxDeviceService
{
    @Autowired
    private ZyxDeviceMapper zyxDeviceMapper;
    @Autowired
    private ZyxSendorderMapper  zyxSendorderMapper;
    /**
     * 查询设备基础信息
     *
     * @param id 设备基础信息ID
     * @return 设备基础信息
     */
    @Override
    public ZyxDevice selectZyxDeviceById(Long id)
    {
        return zyxDeviceMapper.selectZyxDeviceById(id);
    }

    /**
     * 查询设备基础信息列表
     *
     * @param zyxDevice 设备基础信息
     * @return 设备基础信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "")
    public List<ZyxDevice> selectZyxDeviceList(ZyxDevice zyxDevice)
    {
        return zyxDeviceMapper.selectZyxDeviceList(zyxDevice);
    }

    @Override
    public List<ZyxDevice> selectNoSelZyxDeviceList(ZyxDevice zyxDevice) {
        return zyxDeviceMapper.selectNoSelZyxDeviceList(zyxDevice);
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "")
    public List<ZyxDevice> zyxDeviceFaultlist(ZyxDevice zyxDevice) {
        return zyxDeviceMapper.zyxDeviceFaultlist(zyxDevice);
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "")
    public List<ZyxDevice> zyxDeviceCostlist(ZyxDevice zyxDevice) {
        return zyxDeviceMapper.zyxDeviceCostlist(zyxDevice);
    }

    @Override
    public List<ZyxDevice> selectDeviceTj(ZyxDevice zyxDevice) {
        return zyxDeviceMapper.selectDeviceTj(zyxDevice);
    }


    /**
     * 查询系统菜单列表
     *
     * @param zyxDevice 菜单信息
     * @return 菜单列表
     */
    @Override
    public List<ZyxDevice> selectZyxDeviceList(ZyxDevice zyxDevice, Long userId)
    {
        List<ZyxDevice> deviceList = null;
        // 管理员显示所有菜单信息
        if (SysUser.isAdmin(userId))
        {
            deviceList = zyxDeviceMapper.selectZyxDeviceList(zyxDevice);
        }

        return deviceList;
    }
    /**
     * 构建前端所需要下拉树结构
     *
     * @param devices 菜单列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDevicesTreeSelect(List<ZyxDevice> devices)
    {
        List<ZyxDevice> menuTrees = buildDeviceTree(devices);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
//        return new List<TreeSelect>;
    }

    /**
     * 构建前端所需要树结构
     *
     * @param devices 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<ZyxDevice> buildDeviceTree(List<ZyxDevice> devices)
    {
        List<ZyxDevice> returnList = new ArrayList<ZyxDevice>();
        List<Long> tempList = new ArrayList<Long>();
        for (ZyxDevice dept : devices)
        {
            tempList.add(dept.getId());
        }
        for (Iterator<ZyxDevice> iterator = devices.iterator(); iterator.hasNext();)
        {
            ZyxDevice menu = (ZyxDevice) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(devices, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = devices;
        }
        return returnList;
    }
    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<ZyxDevice> list, ZyxDevice t)
    {
        // 得到子节点列表
        List<ZyxDevice> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ZyxDevice tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<ZyxDevice> getChildList(List<ZyxDevice> list, ZyxDevice t)
    {
        List<ZyxDevice> tlist = new ArrayList<ZyxDevice>();
        Iterator<ZyxDevice> it = list.iterator();
        while (it.hasNext())
        {
            ZyxDevice n = (ZyxDevice) it.next();
            if (n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ZyxDevice> list, ZyxDevice t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
    /**
     * 新增设备基础信息
     *
     * @param zyxDevice 设备基础信息
     * @return 结果
     */
    @Override
    public int insertZyxDevice(ZyxDevice zyxDevice)
    {
        zyxDevice.setCreateTime(DateUtils.getNowDate());
        return zyxDeviceMapper.insertZyxDevice(zyxDevice);
    }

    /**
     * 修改设备基础信息
     *
     * @param zyxDevice 设备基础信息
     * @return 结果
     */
    @Override
    public int updateZyxDevice(ZyxDevice zyxDevice)
    {
        zyxDevice.setUpdateTime(DateUtils.getNowDate());
        return zyxDeviceMapper.updateZyxDevice(zyxDevice);
    }

    /**
     * 批量删除设备基础信息
     *
     * @param ids 需要删除的设备基础信息ID
     * @return 结果
     */
    @Override
    public int deleteZyxDeviceByIds(Long[] ids)
    {
        return zyxDeviceMapper.deleteZyxDeviceByIds(ids);
    }

    @Override
    public boolean checkDeviceIsUse(Long[] ids) {
        boolean ret =false;
        for (Long id : ids) {
            ZyxDevice zyxDevice= zyxDeviceMapper.selectZyxDeviceById(id);
            if(null!=zyxDevice.getIsallot()&&zyxDevice.getIsallot().equals("Y")){
                ret=true;
                break;
            }else if(null!=zyxDevice.getIscheck()&&zyxDevice.getIscheck().equals("Y")){
                ret=true;
                break;
            }else if(null!=zyxDevice.getIsrecord()&&zyxDevice.getIsrecord().equals("Y")){
                ret=true;
                break;
            }else if(null!=zyxDevice.getIstake()&&zyxDevice.getIstake().equals("Y")){
                ret=true;
                break;
            }else if(null!=zyxDevice.getIstake()&&zyxDevice.getIstake().equals("Y")){
                ret=true;
                break;
            }
            ZyxSendorder zyxSendorder=new ZyxSendorder();
            zyxSendorder.setDeviceid(id);
            List<ZyxSendorder> zyxSendorders=zyxSendorderMapper.selectZyxSendorderList(zyxSendorder);
            if(zyxSendorders.size()>0){
                ret=true;
                break;
            }
        }
        return ret;
    }

    /**
     * 删除设备基础信息信息
     *
     * @param id 设备基础信息ID
     * @return 结果
     */
    @Override
    public int deleteZyxDeviceById(Long id)
    {
        return zyxDeviceMapper.deleteZyxDeviceById(id);
    }
}
